# if enable debug mode: 0-disable, 1-enable
DEBUG ?= 0
# available platforms: linux-amd64, linux-arm64, darwin-amd64, darwin-arm64, windows-amd64
PLATFORM ?= linux-amd64
# harbor information
HARBOR_URL ?= 127.0.0.1:80
HARBOR_USER ?= admin
HARBOR_PASSWORD ?= Harbor12345
HARBOR_PROJECT_NAME ?= library

GOPROXY ?= goproxy.cn,direct

ifeq ($(DEBUG), 0)
	GC_FLAGS :=
else ifeq ($(DEBUG), 1)
	GC_FLAGS := -gcflags "all=-N -l"
else
    $(error unknown debug mode: $(DEBUG))
endif

ifeq ($(PLATFORM), linux-amd64)
	OS:=linux
	ARCH:=amd64
else ifeq ($(PLATFORM), linux-arm64)
	OS:=linux
	ARCH:=arm64
else ifeq ($(PLATFORM), darwin-amd64)
	OS:=darwin
	ARCH:=amd64
else ifeq ($(PLATFORM), darwin-arm64)
	OS:=darwin
	ARCH:=arm64
else ifeq ($(PLATFORM), windows-amd64)
	OS:=windows
	ARCH:=amd64
else
    $(error unknown platform: $(PLATFORM))
endif

COMPILE_OPTION := CGO_ENABLED=0 GOOS=$(OS) GOARCH=$(ARCH)

APP_NAME := das
APP_BIN := ./bin/$(APP_NAME)-$(PLATFORM)
GIT_TAG := $(shell git describe --tags --always --dirty)
BUILD_TIME := $(shell date "+%Y-%m-%d %H:%M:%S")
FULL_COMMIT := $(shell git rev-parse HEAD)
GIT_BRANCH  := $(shell git rev-parse --abbrev-ref HEAD)

LD_FLAGS := -ldflags " \
	-X 'github.com/romberli/das/config.AppName=$(APP_NAME)' \
	-X 'github.com/romberli/das/config.Version=$(GIT_TAG)' \
	-X 'github.com/romberli/das/config.BuildTime=$(BUILD_TIME)' \
	-X 'github.com/romberli/das/config.FullCommit=$(FULL_COMMIT)' \
	-X 'github.com/romberli/das/config.Branch=$(GIT_BRANCH)' \
"

SOAR_NAME := soar
SOAR_BIN := ./bin/$(SOAR_NAME)-$(PLATFORM)
SOAR_VERSION := 0.11.1-rc.2
SOAR_DOWNLOAD_FILE := soar-$(SOAR_VERSION)-$(PLATFORM).tgz
SOAR_DOWNLOAD_URL := https://github.com/romberli/soar/releases/download/$(SOAR_VERSION)/$(SOAR_DOWNLOAD_FILE)

BASE_IMAGE ?= alpine:3.16.0

.PHONY: default
default: build

.PHONY:
all: build docker

.PHONY: prepare
GO_VERSION_MIN=1.16
VER_TO_INT:=awk '{split(substr($$0, match ($$0, /[0-9\.]+/)), a, "."); print a[1]*10000+a[2]*100+a[3]}'
prepare:
	@echo "preparing started..."
	@mkdir -p ./bin
	@if test $(shell go version | $(VER_TO_INT) ) -lt \
	$(shell echo "$(GO_VERSION_MIN)" | $(VER_TO_INT)); \
	then echo "go version $(GO_VERSION_MIN)+ required, found: "; go version; exit 1; \
		else echo "preparing completed.";	fi

.PHONY: soar
soar: prepare
	@if [[ ! -f "$(SOAR_BIN)" ]] ; then \
		echo "soar version $(SOAR_VERSION) is not installed" ; \
	    echo "downloading soar version $(SOAR_VERSION)" ; \
		wget $(SOAR_DOWNLOAD_URL) -O ./bin/$(SOAR_DOWNLOAD_FILE) ;\
		tar zxvf ./bin/$(SOAR_DOWNLOAD_FILE) -C ./bin ; \
		rm -f ./bin/$(SOAR_DOWNLOAD_FILE) ; \
		echo "soar version $(SOAR_VERSION) installed." ; \
	fi

.PHONY: build
build: prepare soar
	@echo "building started..."
	@export  GO111MODULE=on
	@export  GOPROXY=$(GOPROXY)
	$(COMPILE_OPTION) go build -v $(GC_FLAGS) $(LD_FLAGS) -o $(APP_BIN) main.go
	@echo "building completed."

.PHONY: docker
docker: build
	@echo "building docker image started..."
	@mkdir -p build/bin && mkdir -p build/config && mkdir -p build/log
	@cp $(APP_BIN) build/bin/$(APP_NAME)
	@cp $(SOAR_BIN) build/bin/$(SOAR_NAME)
	@cp ./config/$(APP_NAME)_docker.yaml build/config/$(APP_NAME).yaml
	@cp ./config/$(SOAR_NAME)_docker.yaml build/config/$(SOAR_NAME).yaml
	@cp ./config/$(SOAR_NAME).blacklist build/config/$(SOAR_NAME).blacklist
	@cp -r ./script build/
	docker buildx build --platform=$(OS)/$(ARCH) --build-arg BASE_IMAGE=$(BASE_IMAGE) --tag ${APP_NAME}:$(GIT_TAG) ./
	@echo "building docker image completed."

.PHONY: harbor
harbor: docker
	@echo "pushing docker image to harbor started..."
	docker login -u $(HARBOR_USER) -p $(HARBOR_PASSWORD) $(HARBOR_URL)
	docker tag ${APP_NAME}:$(GIT_TAG) $(HARBOR_URL)/$(HARBOR_PROJECT_NAME)/$(APP_NAME):$(GIT_TAG)
	docker push $(HARBOR_URL)/$(HARBOR_PROJECT_NAME)/$(APP_NAME):$(GIT_TAG)
	docker logout $(HARBOR_URL)
	@echo "pushing docker image to harbor completed."

# Cleans our projects: deletes binaries
.PHONY: clean
clean:
	@echo "cleaning files created during building started..."
	rm -f ./bin/*
	rm -f ./build/*
	@echo "cleaning files created during building started."
